package com.example.dev_demo.mybatis.generator;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.Context;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class MyBatisGeneratorRunner {

    public static void main(String[] args) {
        try {
            // 1. 加载generatorConfig.xml配置文件
            InputStream configStream = MyBatisGeneratorRunner.class
                    .getResourceAsStream("/generatorConfig.xml");
            // 2. 获取用户输入的表名
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入要生成的表名(多个表用逗号分隔):");
            String tableNames = scanner.nextLine();
            scanner.close();

            // 3. 解析配置并动态添加表
            List<String> warnings = new ArrayList<>();
            ConfigurationParser cp = new ConfigurationParser(warnings);
            Configuration config = cp.parseConfiguration(configStream);

            // 动态添加表配置
            String[] tables = tableNames.split(",");
            for (String tableName : tables) {
                if (!tableName.trim().isEmpty()) {
                    final Context context = config.getContexts().get(0);
                    context.addTableConfiguration(
                            createTableConfiguration(tableName.trim(), context)
                    );
                }
            }

            // 4. 生成代码
            boolean overwrite = true;
            DefaultShellCallback callback = new DefaultShellCallback(overwrite);
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
            myBatisGenerator.generate(null);

            // 5. 输出警告信息
            for (String warning : warnings) {
                System.out.println(warning);
            }

            System.out.println("代码生成完成!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static org.mybatis.generator.config.TableConfiguration createTableConfiguration(String tableName, Context context) {
        org.mybatis.generator.config.TableConfiguration tableConfig = new org.mybatis.generator.config.TableConfiguration(context);
        tableConfig.setTableName(tableName);
        tableConfig.setDomainObjectName(convertToCamelCase(tableName));
        //以下配置是否要生成例子
        tableConfig.setCountByExampleStatementEnabled(false);
        tableConfig.setUpdateByExampleStatementEnabled(false);
        tableConfig.setDeleteByExampleStatementEnabled(false);
        tableConfig.setSelectByExampleStatementEnabled(false);
        return tableConfig;
    }

    private static String convertToCamelCase(String tableName) {
        // 将下划线命名转换为驼峰命名
        StringBuilder result = new StringBuilder();
        String[] parts = tableName.split("_");
        for (String part : parts) {
            if (!part.isEmpty()) {
                result.append(Character.toUpperCase(part.charAt(0)))
                        .append(part.substring(1).toLowerCase());
            }
        }
        return result.toString();
    }
}